/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-04-11 09:42:08
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-07-19 09:24:49
 * @FilePath: \vite-project\src\hooks\basicSystem\price\usePriceStandard.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { cartypeTreeWithPriceRule, getCarTypeTree } from '@/apis/basicSystem/carType';
import { getystemDataPriceList } from '@/apis/basicSystem/systemData/pricing';
import { getystemDataUnitList } from '@/apis/basicSystem/systemData/unitPrice';
import { useCommonStore } from '@/store/common';
import { useEffect, useState } from 'react';

export default function usePriceStandard() {
	const [standards, setStandards] = useState<any[]>([]);
	const [originTreeData, setOriginTreeData] = useState<any[]>([]);
	const [treeData, setTreeData] = useState<any[]>([]);
	const [disabledUnSelectedTreeData, setDisabledUnSelectedTreeData] = useState<any[]>([]);
	const [loading, setLoading] = useState(false);
	const { getSpecification } = useCommonStore();
	const [range, setRange] = useState<{
		startTime?: string;
		endTime?: string;
	} | null>();
	// 规格单位
	const [valueUnitList, setValueUnitList] = useState<
		{
			name?: string;
			specification?: string;
		}[]
	>([]);
	// 计价
	const [valuePriceList, setValuePriceList] = useState<any[]>([]);

	const traverseTree = (
		node: string | any[] | null,
		visit: { (node: { cartypeId: string; disabled: boolean }): void; (arg0: any): void },
	) => {
		if (node === null) {
			return;
		}
		for (let i = 0; i < node?.length; i++) {
			visit(node?.[i]);
			if (node?.[i]?.children?.length) {
				traverseTree(node?.[i]?.children, visit);
			}
		}
	};

	// 禁用未选类型
	const disabledUnSelectedTreeDataFunc = (tree: any[]) => {
		let newTreeData = JSON.parse(JSON.stringify(tree));
		let enanbleTypeList: string[] = [];
		standards.map(item =>
			item?.carTypes?.map(car => {
				enanbleTypeList.push(car.cartypeId);
			}),
		);
		if (enanbleTypeList?.length) {
			traverseTree(newTreeData, node => {
				if (enanbleTypeList.includes(node?.id)) {
					node.disabled = false;
				} else {
					node.disabled = true;
				}
			});
		}
		return newTreeData;
	};

	// 禁用已选类型
	const disabledSelectedTreeData = (tree: any[]) => {
		let newTreeData = JSON.parse(JSON.stringify(tree));
		let disabledTypeList: string[] = [];
		standards.map(item =>
			item?.carTypes?.map(car => {
				disabledTypeList.push(car.cartypeId);
			}),
		);
		if (disabledTypeList?.length) {
			traverseTree(newTreeData, node => {
				if (disabledTypeList.includes(node?.id)) {
					node.disabled = true;
				}
			});
		}
		return newTreeData;
	};

	// 获取时间范围内类型树
	const getTreeData = (startTime?: string, endTime?: string) => {
		setLoading(true);
		cartypeTreeWithPriceRule({
			beginDate: startTime,
			endDate: endTime,
		})
			.then(res => {
				const curTreeData = [
					{
						...(res?.typeTree || {}),
						disabled: true,
					},
				];
				setOriginTreeData([
					{
						...(res?.typeTree || {}),
						disabled: true,
					},
				]);
				const newTreeData = disabledSelectedTreeData(JSON.parse(JSON.stringify(curTreeData)));
				const newTreeData2 = disabledUnSelectedTreeDataFunc(JSON.parse(JSON.stringify(newTreeData)));
				// 获取类型树
				setTreeData(newTreeData);
				setDisabledUnSelectedTreeData(newTreeData2);
			})
			.finally(() => {
				setLoading(false);
			});
	};

	useEffect(() => {
		// 初始化数据
		getSpecification('CarPriceEvaluateSpecification');
		// 获取规格单位
		getystemDataUnitList({
			pageNumber: 1,
			pageSize: 9999,
		}).then(res => {
			setValueUnitList(res?.recordDatas || []);
		});

		// 获取价格单位
		getystemDataPriceList({
			pageNumber: 1,
			pageSize: 9999,
		}).then(res => {
			setValuePriceList(res?.recordDatas);
		});
	}, []);

	return {
		loading,
		standards,
		setStandards,
		treeData,
		disabledUnSelectedTreeData,
		originTreeData,
		valueUnitList,
		valuePriceList,
		getTreeData,
		disabledSelectedTreeData,
		range,
		setRange,
	};
}
